home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 21 / CU Amiga Magazine's Super CD-ROM 21 (1998)(EMAP Images)(GB)[!][issue 1998-04].iso / CUCD / Magazine / C_Tutorial / Part-6 / asl2 / loadsave.c < prev    next >
C/C++ Source or Header  |  1997-10-27  |  3KB  |  123 lines

  1. #include "loadsave.h"
  2. #include "bitmap.h"
  3. #include "drawwin.h"
  4.  
  5. #include "iff.h"
  6.  
  7. #include<libraries/asl.h>
  8.  
  9. #include<string.h>
  10. #include<stdio.h>
  11.  
  12. #include<clib/asl_protos.h>
  13. #include<clib/dos_protos.h>
  14. #include<clib/graphics_protos.h>
  15.  
  16. /* The size of our filename string */
  17. #define MAXFILENAME        (300)
  18.  
  19. /* Global handles for our requesters */
  20. static struct FileRequester* loadreq = NULL;
  21. static struct FileRequester* savereq = NULL;
  22.  
  23. /* Open an ASL load file requester */
  24. void load(struct Window* win)
  25. {
  26.     /* Allocate the requester if we haven't already */
  27.     if(loadreq == NULL)
  28.         loadreq = (struct FileRequester*)
  29.             AllocAslRequestTags(ASL_FileRequest,
  30.                                                     ASLFR_TitleText,            "Load File",
  31.                                                     ASLFR_Flags1,                    FRF_DOPATTERNS,
  32.                                                     ASLFR_InitialPattern,    "#?.iff",
  33.                                                     TAG_DONE);
  34.     if(loadreq)
  35.     {
  36.         if(AslRequestTags(loadreq, ASLFR_Window, win, TAG_DONE))
  37.         {
  38.             char filename[MAXFILENAME];
  39.             /* Create complete filename from ASL's dir and file */
  40.             strcpy(filename, loadreq->rf_Dir);
  41.             if(AddPart(filename, loadreq->rf_File, MAXFILENAME))
  42.             {
  43.                 IFFL_HANDLE handle;
  44.                 /* Try to open the IFF file */
  45.                 if(handle = IFFL_OpenIFF(filename, IFFL_MODE_READ))
  46.                 {
  47.                     LONG  count;
  48.                     UWORD colortable[256];
  49.                     /* Get colour information and change screen colours */
  50.                     count = IFFL_GetColorTab(handle, colortable);
  51.                     LoadRGB4(&(win->WScreen->ViewPort), colortable, count);
  52.                     /* If we can load the picture, update window's display */
  53.                     if(IFFL_DecodePic(handle, getBitmap()))
  54.                         CopySBitMap(win->WLayer);
  55.                     else
  56.                         printf("Error: could not decode IFF picture\n");
  57.                     IFFL_CloseIFF(handle);
  58.                 }
  59.                 else
  60.                     printf("Error: could not open IFF file\n");
  61.             }
  62.             else
  63.                 printf("Error: could not make filename\n");
  64.         }
  65.         /* else: requester was cancelled */
  66.     }
  67.     else
  68.         printf("Error: could not allocate ASL (load) file request\n");
  69. }
  70.  
  71. /* Open an ASL save file requester */
  72. void save(struct Window* win)
  73. {
  74.     /* Another way of saying "allocate if we haven't already" */
  75.     if(savereq ||
  76.         (savereq = (struct FileRequester*)
  77.             AllocAslRequestTags(ASL_FileRequest,
  78.                                                     ASLFR_TitleText,            "Save File",
  79.                                                     ASLFR_Flags1,                    FRF_DOPATTERNS | FRF_DOSAVEMODE,
  80.                                                     ASLFR_InitialPattern,    "#?.iff",
  81.                                                     ASLFR_InitialFile,        "picture.iff",
  82.                                                     TAG_DONE)))
  83.     {
  84.         if(AslRequestTags(savereq, ASLFR_Window, win, TAG_DONE))
  85.         {
  86.             char filename[MAXFILENAME];
  87.             /* Create complete filename from ASL's dir and file */
  88.             strcpy(filename, savereq->rf_Dir);
  89.             if(AddPart(filename, savereq->rf_File, MAXFILENAME))
  90.             {
  91.                 /* Make sure our bitmap is the same as the display */
  92.                 SyncSBitMap(win->WLayer);
  93.                 /* Try saving our bitmap, using the screen's colours */
  94.                 if(IFFL_SaveBitMap(filename, getBitmap(),
  95.                                                     win->WScreen->ViewPort.ColorMap->ColorTable,
  96.                                                     IFFL_COMPR_BYTERUN1) == 0)
  97.                     printf("Error: could not write IFF picture\n");
  98.             }
  99.             else
  100.                 printf("Error: could not make filename\n");
  101.         }
  102.         /* else: requester was cancelled */
  103.     }
  104.     else
  105.         printf("Error: could not allocate ASL (save) file request\n");
  106. }
  107.  
  108. /* Free any requesters that may have been allocated */
  109. void freeReqs()
  110. {
  111.     if(loadreq)
  112.     {
  113.         FreeAslRequest(loadreq);
  114.         loadreq = NULL;
  115.     }
  116.     if(savereq)
  117.     {
  118.         FreeAslRequest(savereq);
  119.         savereq = NULL;
  120.     }
  121. }
  122.  
  123.